using System;

namespace JSC.Portal.Entities
{
	public partial class ModuleProperty : DescriptiveEntity
	{
		public Type GetRealPropertyType()
		{
			if (PropertyType != null)
			{
				if (!PropertyType.StartsWith("System"))
				{
					// Use the assemblyname of the ModuleType to find the custom type
					string fullName = string.Format("{0}, {1}", PropertyType, ModuleType.AssemblyName);
					Type realType = Type.GetType(fullName);
					if (realType == null)
					{
						throw new NullReferenceException(String.Format("The custom type {0} was not found in assembly {1}.", 
							PropertyType, ModuleType.AssemblyName));
					}
					else
					{
						return realType;
					}
				}
				else
				{
					Type realType = Type.GetType(PropertyType, true, true);
					if (realType == null)
					{
						throw new NullReferenceException(String.Format("The CLR type {0} is invalid.", PropertyType));
					}
					else
					{
						return realType;
					}
				}
			}
			else
			{
				throw new NullReferenceException("Unable to get the type of the module setting");
			}
		}
	}
}
